package pres.wchen.bussiness.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import pres.wchen.bussiness.cor.model.BaseModel;
import pres.wchen.bussiness.vo.SysUserVO;
import pres.wchen.bussiness.service.SysUserService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import pres.wchen.bussiness.web.BaseController;

/**
 * @作者： wchen
 * @描述： 用户表管理
 * @创建时间： 2018/02/16 13:30:30
 * @版本：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息：
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends BaseController {
	@Autowired
	private SysUserService sysUserService;


	/**
     *  @描述: 用户表新增
     *  @参数： [sysUserVO]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/16
     *  @修改人和其它信息:
     */
	@PostMapping(value = "/add")
	public Map insert(SysUserVO sysUserVO) {
		Map map = new HashMap();
		map.put("username", sysUserVO.getUsername());
		BaseModel baseModel = sysUserService.list(map);
		if(baseModel.getList().size() > 0){
			return super.commonFailResult("用户名重复");
		}
		Long id = sysUserService.insert(sysUserVO);
		return super.dealInsertResult(id);
	}


	/**
     *  @描述: 用户表删除
     *  @参数： [id]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/16
     *  @修改人和其它信息:
     */
	@DeleteMapping(value = "/delete/{id}")
	public Map delete(@PathVariable("id") Long id) {
		Integer result = sysUserService.delete(id);
		return super.dealDeleteResult(result);
	}


	/**
     *  @描述: 用户表修改
     *  @参数： [sysUserVO]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/16
     *  @修改人和其它信息:
     */
	@PutMapping(value = "/modify/")
	public Map modify(SysUserVO sysUserVO) {
		Map map = new HashMap();
		map.put("username", sysUserVO.getUsername());
		map.put("exitId", sysUserVO.getId());
		BaseModel baseModel = sysUserService.list(map);
		if(baseModel.getList().size() > 0){
			return super.commonFailResult("用户名重复");
		}
		Integer result = sysUserService.update(sysUserVO);
		return super.dealUpdateResult(result);
	}


	/**
     *  @描述: 用户表详情
     *  @参数： [id]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/16
     *  @修改人和其它信息:
     */
	@GetMapping(value = "/info/{id}")
	public ModelAndView info(@PathVariable("id") Long id) {
		SysUserVO sysUserVO = sysUserService.info(id);
		return super.dealInfoResult("/sys/user/maintain_modify", sysUserVO);
	}


	/**
     *  @描述: 用户表分页列表
     *  @参数： [request]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/16
     *  @修改人和其它信息:
     */
	@GetMapping(value = "/pageList")
	public ModelAndView pageList(HttpServletRequest request) {
		Object pageSize = request.getParameter("pageSize");
		Object pageNo = request.getParameter("pageNo");
		Object username = request.getParameter("username");
		Object pageFlag = request.getParameter("pageFlag");
		System.out.println("userName: " + username);
		Map conditionMap = new HashMap();
		conditionMap.put("pageSize", pageSize);
		conditionMap.put("pageNo", pageNo);
		conditionMap.put("username", username);
		conditionMap.put("pageFlag", pageFlag);
		BaseModel baseModel = sysUserService.list(conditionMap);
		System.out.println(baseModel.toString());
		return super.dealListResult("/sys/user/maintain", baseModel);
	}


	/*****************************************************个人中心**********************************************/

	/**
	 *
	 * @描述： 个人资料
	 * @名称： userInfo
	 * @入参： [sysUser, session]
	 * @出参： org.springframework.web.servlet.ModelAndView
	 * @修改信息： 创建：wchen 2018/3/7
	 */
	@GetMapping("/userInfo")
	public ModelAndView userInfo(HttpServletRequest request, HttpSession session){

		String type = request.getParameter("type");
		SysUserVO sysUserVO = (SysUserVO) session.getAttribute("sysUser");
		if(sysUserVO != null){
			session.setAttribute("sysUser", sysUserVO);
			if(type == null){
				return super.dealInfoResult("/sys/user/info",sysUserVO);
			}else{
				return super.dealInfoResult("/sys/user/info_modify",sysUserVO);
			}
		}else{
			System.out.println("========================session 不存在用户========================");
			return new ModelAndView("/sys/login");
		}
	}



	/**
	 *
	 * @描述： 个人资料更新
	 * @名称： userInfo
	 * @入参： [sysUser, session]
	 * @出参： org.springframework.web.servlet.ModelAndView
	 * @修改信息： 创建：wchen 2018/3/7
	 */
	@PutMapping("/userInfoModify")
	public Map user(SysUserVO sysUserVO, HttpSession session){
		sysUserService.update(sysUserVO);
		SysUserVO sessionSysUserVO = (SysUserVO) session.getAttribute("sysUser");
		sessionSysUserVO.setPassword(sysUserVO.getPassword());
		sessionSysUserVO.setUsername(sysUserVO.getUsername());

		session.setAttribute("sysUser", sessionSysUserVO);
		return super.dealInfoResult(sessionSysUserVO);
	}

}
